/*===============================
 *Title      :  MediaExplorerActivity.java
 *Created  :[ 2012-3-23 下午1:31:30 ] by fanlt
 * ==============================
 * Description:   
 * Copyright  :  Copyright (c) 2012
 * Company    :  Flyfot
 * @author  <a href="fanlt@flyfot.cn">fanlt</a>
 *
 */
package org.zxstudio.encrypt;

import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.zxstudio.encrypt.utils.SDUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;

/**
 * @author fan
 * 
 */
public class MediaExplorerActivity extends Activity implements OnClickListener,
		OnItemClickListener {

	private static final String TAG = "MediaExplorerActivity";
	private static final boolean DEBUG = true;

	private MediaCursorAdapter mAdapter;
	private Cursor mCursor = null;

	private Button mImage;
	private Button mVideo;
	private Button mAudio;
	private GridView mGridView;
	private Button mEncryptBtn;
	private Button mCancelBtn;

	private static BlockingQueue<Integer> mTasks = new LinkedBlockingQueue<Integer>(
			1);

	private ExplorerMediaTask mSyncTask;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 0:
				mAdapter.changeCursor(mCursor);
				mAdapter.notifyDataSetChanged();
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.multi_media_view);
		initRes();

	}

	/**
	 * 
	 */
	private void initRes() {
		mImage = (Button) findViewById(R.id.image);
		mImage.setOnClickListener(this);
		mVideo = (Button) findViewById(R.id.video);
		mVideo.setOnClickListener(this);
		mAudio = (Button) findViewById(R.id.audio);
		mAudio.setOnClickListener(this);
		mGridView = (GridView) findViewById(R.id.gridview);
		mGridView.setOnItemClickListener(this);
		mEncryptBtn = (Button) findViewById(R.id.encrypt);
		mCancelBtn = (Button) findViewById(R.id.cancel);

		mAdapter = new MediaCursorAdapter(this, mCursor);
		mAdapter.setMediaType(MediaBean.IMAGE);
		mGridView.setAdapter(mAdapter);
		mSyncTask = new ExplorerMediaTask();
		mSyncTask.execute(MediaBean.IMAGE);
	}

	private class ExplorerMediaTask extends Thread {

		int mediaType = 0;

		public ExplorerMediaTask() {
			this.start();
		}

		public void execute(int type) {
			mediaType = type;
			try {
				mTasks.put(mediaType);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

		@Override
		public void run() {
			while (true) {
				try {
					int mediaType = mTasks.take();
					mAdapter.setMediaType(mediaType);
					switch (mediaType) {
					case MediaBean.IMAGE:
						queryImage();
						break;
					case MediaBean.VIDEO:
						queryVideo();
						break;
					case MediaBean.AUDIO:
						queryAudio();
						break;
					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					continue;
				}
			}
		}
	}

	/**
	 * 查询SD卡中所有图片
	 */
	private void queryImage() {
		ContentResolver contentResolver = getContentResolver();
		String[] projection = new String[] { MediaStore.Images.Media._ID,
				MediaStore.Images.Media.TITLE, MediaStore.Images.Media.DATA,
				MediaStore.Images.Media.SIZE };
		if (mCursor != null) {
			mCursor.close();
		}
		mCursor = contentResolver.query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
				null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
		mHandler.sendEmptyMessage(0);
	}

	/**
	 * 查询SD卡中所有视频
	 */
	private void queryVideo() {
		if (mCursor != null) {
			mCursor.close();
		}
		ContentResolver contentResolver = getContentResolver();
		String[] projection = new String[] { MediaStore.Video.Media._ID,
				MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DATA };
		mCursor = contentResolver.query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null,
				null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
		mHandler.sendEmptyMessage(0);
	}

	/**
	 * 查询SD卡中所有音频
	 */
	private void queryAudio() {
		
		if (mCursor != null) {
			mCursor.close();
		}
		ContentResolver contentResolver = getContentResolver();
		String[] projection = new String[] { MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA };
		mCursor = contentResolver.query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null,
				null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		mHandler.sendEmptyMessage(0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.image:
			mSyncTask.execute(MediaBean.IMAGE);
			break;
		case R.id.video:
			mSyncTask.execute(MediaBean.VIDEO);
			break;
		case R.id.audio:
			mSyncTask.execute(MediaBean.AUDIO);
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget
	 * .AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position,
			long id) {
		mAdapter.updateSelItem(view,id);
	}
}
